package com.example.hblog.module.controller;

import com.example.hblog.Result;
import com.example.hblog.module.pojo.Remember;
import com.example.hblog.module.service.RememberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("api")
public class RememberController {

    @Autowired
    private RememberService rememberService;

    /*发布备忘录*/
    @PostMapping("/addRemember")
    public Result addRemember(@RequestBody Remember remember){
        try{
            String id = (UUID.randomUUID()+"").replace("-","");
            remember.setId(id);
            Date date=new Date();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            remember.setTime(dateFormat.format(date));
            rememberService.addRemember(remember);
            return new Result(200,"记住了",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(400,"没记住",null);

        }
    }

    @ResponseBody
    @GetMapping("/allRemember")
    public List<Remember> allRemember() {
        return rememberService.allRemember();
    }

    @ResponseBody
    @GetMapping("/someRemember")
    public List<Remember> someRemember() {
        return rememberService.limitRemember();
    }

    @ResponseBody
    @GetMapping("/deleteRemember")
    public Result deleteRemember(String id) {
        try{
            rememberService.deleteRemember(id);
            return new Result(200,"删除成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(400,"删除失败",null);
        }
    }

    @ResponseBody
    @GetMapping("/clearRemember")
    public Result clearRemember() {
        try{
            rememberService.clearRemember();
            return new Result(200,"已清空备忘录",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(400,"清空失败！",null);
        }
    }
}
